import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_study_app/app/router/router_param.dart';
import 'package:flutter_study_app/module/check_visit_record_page.dart';
import 'package:flutter_study_app/module/home_page.dart';
import 'package:flutter_study_app/module/image_watermark_page.dart';
import 'package:flutter_study_app/module/marketing_section_page.dart';
import 'package:flutter_study_app/module/online_lend/page/online_lend_white_list_page.dart';
import 'package:flutter_study_app/module/questionnaire_create_page.dart';
import 'package:flutter_study_app/module/return_visit_record_page.dart';
import 'package:flutter_study_app/module/special_region/page/storage_special_region_page.dart';

import 'i_router.dart';
import 'page_builder.dart';

class Routers {
  static late FluroRouter router;
  static Map<String, PageBuilder> pageRouters = {};

  static const String homePage = "/homePage";
  static const String visitPage = "/visitPage";
  static const String statisticsPage = "/statisticsPage";
  static const String returnVisitRecordPage = "/returnVisitRecordPage";
  static const String storageSpecialRegionPage = "/storageSpecialRegionPage";
  static const String storageSpecialRegionSearchPage =
      "/storageSpecialRegionSearchPage";
  static const String questionnaireCreatePage = "/questionnaireCreatePage";
  static const String onlineLendWhiteListPage = "/onlineLendWhiteListPage";
  static const String onlineLendSearchPage = "/onlineLendSearchPage";

  //立案清算
  static const String caseClearListPage = "/caseClearListPage";
  static const String caseClearEditPage = "/caseClearEditPage";
  static const String caseClearDetailPage = "/caseClearDetailPage";

  //档案管理
  static const String archivesStatisticsPage = "/archivesStatisticsPage";
  static const String archivesStatisticsDetailPage =
      "/archivesStatisticsDetailPage";

  //营销专区
  static const String marketingSectionPage = "/marketingSectionPage";

  static const String checkVisitRecordPage = "/checkVisitRecordPage";

  static const String markdownPage = "/markdownPage";

  static const String imageWatermarkPage = "/imageWatermarkPage";

  static void initRouters(List<IRouter> listRouter) {
    router = FluroRouter();
    _configRouters(router, listRouter);
  }

  static void _configRouters(FluroRouter router, List<IRouter> listRouter) {
    //首页路由
    router.define(homePage,
        handler: Handler(handlerFunc: (context, params) => HomePage()));

    router.define(returnVisitRecordPage,
        handler:
            Handler(handlerFunc: (context, params) => ReturnVisitRecordPage()));

    router.define(checkVisitRecordPage,
        handler:
            Handler(handlerFunc: (context, params) => CheckVisitRecordPage()));

    router.define(questionnaireCreatePage,
        handler: Handler(
            handlerFunc: (context, params) => QuestionnaireCreatePage()));

    router.define(onlineLendWhiteListPage,
        handler: Handler(
            handlerFunc: (context, params) => OnlineLendWhiteListPage()));

    router.define(marketingSectionPage,
        handler:
            Handler(handlerFunc: (context, params) => MarketingSectionPage()));

    router.define(imageWatermarkPage,
        handler:
            Handler(handlerFunc: (context, param) => ImageWatermarkPage()));

    //注册路由
    listRouter.forEach((routerImpl) {
      List<PageBuilder> pages = routerImpl.createPageBuilders();
      pages.forEach((page) {
        router.define(page.path, handler: page.handler);
        pageRouters[page.path] = page;
      });
    });
  }

  static Future navigateTo(BuildContext context, String path,
      {RouterParam? param,
      bool clearStack = false,
      TransitionType transition = TransitionType.cupertino //跳转样式 默认IOS
      }) {
    var pageBuilder = pageRouters[path];
    if (pageBuilder != null) {
      pageBuilder.param = param ?? RouterParam();
    }
    return router.navigateTo(context, path,
        clearStack: clearStack, transition: transition);
  }

  static void navigateToResult(
      BuildContext context, String path, Function(dynamic) onResult,
      {RouterParam? parameters,
      bool clearStack = false,
      TransitionType transition = TransitionType.cupertino}) {
    navigateTo(context, path,
            param: parameters, clearStack: clearStack, transition: transition)
        .then((data) {
      if (data == null) return;
      onResult(data);
    }).catchError((dynamic error) {
//      LogUtil.e('$error');
    });
  }

  /// 返回
  static void goBack(BuildContext context) {
    Navigator.pop(context);
  }

  /// 带参数返回
  static void goBackWithParams(BuildContext context, Object result) {
    Navigator.pop<Object>(context, result);
  }
}
